package webEquipeRechercheGwt.ejbServer.statelessBeans;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Transient;

import webEquipeRechercheGwt.client.entityMirror.PaperMirror;
import webEquipeRechercheGwt.client.entityMirror.PersonMirror;
import webEquipeRechercheGwt.client.entityMirror.SeminarMirror;
import webEquipeRechercheGwt.client.entityMirror.SoftwareMirror;
import webEquipeRechercheGwt.client.entityMirror.TeamMirror;
import webEquipeRechercheGwt.ejbServer.metier.Paper;
import webEquipeRechercheGwt.ejbServer.metier.Person;
import webEquipeRechercheGwt.ejbServer.metier.Seminar;
import webEquipeRechercheGwt.ejbServer.metier.Software;
import webEquipeRechercheGwt.ejbServer.metier.Team;

@Stateless(mappedName="webEquipeRechercheBean")
@Remote(WebEquipeRechercheFacadeRemote.class)
public class WebEquipeRechercheFacade implements WebEquipeRechercheFacadeRemote {


    @PersistenceContext
    private EntityManager em = null;
	
	@Override
	public void addPerson(String name, String firstname, String phone,
			String mail) {
		Person person = new Person();
		person.setName(name);
		person.setFirstname(firstname);
		person.setPhone(phone);
		person.setMail(mail);
		em.persist(person);
	}

	@Override
	public void addPersonToSeminar(Person p, Seminar s) {
		p.addSeminar(s);
		s.addSpeaker(p);
	}

	@Override
	public void addSeminar(String title, String abstrac, String place,Date dateFrom, Date dateUntil, String url4Slides,
			Collection<Person> speakers) {
		Seminar s = createSeminar(title, abstrac, place, dateFrom, dateUntil, url4Slides);
		Iterator<Person> it = speakers.iterator();
		Person cur;
		while(it.hasNext()){
			cur = it.next();
			cur = em.find(Person.class, cur.getId());
			cur.addSeminar(s);
			s.addSpeaker(cur);
		}
		em.persist(s);
	}

	@Override
	public void addSeminar(String title, String abstrac, String place,Date dateFrom, Date dateUntil, String url4Slides) {
		Seminar s = createSeminar(title, abstrac, place, dateFrom, dateUntil, url4Slides);
		em.persist(s);
	}
	
	@Transient
	private Seminar createSeminar(String title, String abstrac, String place,Date dateFrom, Date dateUntil, String url4Slides){
		Seminar s = new Seminar();
		s.setTitle(title);
		s.setAbstrac(abstrac);
		s.setPlace(place);
		s.setDateFrom(dateFrom);
		s.setDateUntil(dateUntil);
		s.setUrl4Slides(url4Slides);
		s.setSpeakers(new ArrayList<Person>());
		return s;
	}

	@Override
	public void createTeam(String name) {
		Team t = new Team();
		t.setName(name);
		t.setStatus("actif");
		t.setUrlPage("coin coin");
		t.setMembers(new ArrayList<Person>());
		em.persist(t);
	}

	@Override
	public Person findPerson(int id) {
		Person p = em.find(Person.class, Integer.valueOf(id));
		if(p!=null) p.fullLoad();
		return p;
	}

	@Override
	public Seminar findSeminar(int id) {
		Seminar s = em.find(Seminar.class, Integer.valueOf(id));
		if(s!=null) s.fullLoad();
		return s;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<PersonMirror> getPersonsMirrors() {
		Query q = em.createNamedQuery("getPersons");
		Collection<Person> queryResult = q.getResultList();
		Collection<PersonMirror> result = new ArrayList<PersonMirror>();
		for(Person p : queryResult){
			result.add(p.getMirror());
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<PaperMirror> getPapersMirrors() {
		Query q = em.createNamedQuery("getPapers");
		Collection<Paper> queryResult = q.getResultList();
		Collection<PaperMirror> result = new ArrayList<PaperMirror>();
		for(Paper p : queryResult){
			result.add(p.getMirror());
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<SeminarMirror> getSeminarsMirrors() {
		Query q = em.createNamedQuery("getSeminars");
		Collection<Seminar> queryResult = q.getResultList();
		Collection<SeminarMirror> result = new ArrayList<SeminarMirror>();
		for(Seminar s : queryResult){
			result.add(s.getMirror());
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<SoftwareMirror> getSoftwaresMirrors() {
		Query q = em.createNamedQuery("getSoftwares");
		Collection<Software> queryResult = q.getResultList();
		Collection<SoftwareMirror> result = new ArrayList<SoftwareMirror>();
		for(Software s : queryResult){
			result.add(s.getMirror());
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<TeamMirror> getTeamsMirrors() {
		Query q = em.createNamedQuery("getTeams");
		Collection<Team> queryResult = q.getResultList();
		Collection<TeamMirror> result = new ArrayList<TeamMirror>();
		for(Team t : queryResult){
			result.add(t.getMirror());
		}
		return result;
	}

	@Override
	public boolean createPaper(PaperMirror newPaper) {
		Paper p = new Paper();
		p.setState(newPaper.getState());
		p.setTitle(newPaper.getTitle());
		p.setUrl4pdf(newPaper.getUrl4pdf());
		em.persist(p);
		return true;
	}

	@Override
	public boolean createPerson(PersonMirror newPerson) {
		Person p = new Person();
		p.setAffiliation(newPerson.getAffiliation());
		p.setFirstname(newPerson.getFirstname());
		p.setMail(newPerson.getMail());
		p.setName(newPerson.getName());
		p.setPhone(newPerson.getPhone());
		em.persist(p);
		return true;
	}

	@Override
	public boolean createSeminar(SeminarMirror newSeminar) {
		Seminar s = new Seminar();
		s.setAbstrac(newSeminar.getAbstrac());
		s.setDateFrom(newSeminar.getDateFrom());
		s.setDateUntil(newSeminar.getDateUntil());
		s.setPlace(newSeminar.getPlace());
		s.setTitle(newSeminar.getTitle());
		s.setUrl4Slides(newSeminar.getUrl4Slides());
		em.persist(s);
		return true;
	}

	@Override
	public boolean createSoftware(SoftwareMirror newSoftware) {
		Software s = new Software();
		s.setDescription(newSoftware.getDescription());
		s.setTitle(newSoftware.getTitle());
		s.setWebsite(newSoftware.getWebsite());
		em.persist(s);
		return true;
	}

	@Override
	public boolean createTeam(TeamMirror newTeam) {
		Team t = new Team();
		t.setMeaning(newTeam.getMeaning());
		t.setName(newTeam.getName());
		t.setStatus(newTeam.getStatus());
		t.setUrlPage(newTeam.getUrlPage());
		em.persist(t);
		return true;
	}

	@Override
	public boolean linkPersonToPaper(PersonMirror p, PaperMirror paper) {
		int personId = p.getId();
		int paperId = paper.getId();
		Person entityPerson = em.find(Person.class, personId);
		Paper entityPaper = em.find(Paper.class, paperId);
		entityPerson.addPaper(entityPaper);
		return true;
	}

	@Override
	public boolean linkPersonToSeminar(PersonMirror p, SeminarMirror s) {
		int personId = p.getId();
		int seminarId = s.getId();
		Person entityPerson = em.find(Person.class, personId);
		Seminar entitySeminar = em.find(Seminar.class, seminarId);
		entityPerson.addSeminar(entitySeminar);
		return true;
	}

	@Override
	public boolean linkPersonToSoftware(PersonMirror p, SoftwareMirror s) {
		int personId = p.getId();
		int softwareId = s.getId();
		Person entityPerson = em.find(Person.class, personId);
		Software entitySoftware = em.find(Software.class, softwareId);
		entityPerson.addSoftware(entitySoftware);
		return true;
	}

	@Override
	public boolean linkPersonToTeam(PersonMirror p, TeamMirror t) {
		int personId = p.getId();
		int teamId = t.getId();
		Person entityPerson = em.find(Person.class, personId);
		Team entityTeam = em.find(Team.class, teamId);
		entityPerson.addTeam(entityTeam);
		return true;
	}
}
